package com.example.searchservice.doc;

import com.example.searchservice.repository.LocaDateTimeEsConverter;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.*;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Map;

@Data
@Document(indexName = "user", createIndex = true)
@Setting(settingPath = "/elasticsearch/ngram-settings.json")
public class UserDocument implements Serializable {

    @Id
    @Field(type = FieldType.Keyword) // 或者 FieldType.Long
    private String id;

//    @Field(type = FieldType.Text, analyzer = "ik_max_word",  // 索引时使用细粒度分词
//            searchAnalyzer = "ik_smart")

    @MultiField(
            mainField = @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart"),
            otherFields = {
                    // Text字段默认不能排序，同时创建keyword子字段用于排序
                    @InnerField(suffix = "keyword", type = FieldType.Keyword),
                    @InnerField(suffix = "ngram", type = FieldType.Text, analyzer = "ngram_analyzer", searchAnalyzer = "ngram_search_analyzer"),
                    @InnerField(suffix = "edge", type = FieldType.Text, analyzer = "edge_ngram_analyzer", searchAnalyzer = "standard")
            })
    private String name;

    @MultiField(
            mainField = @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart"),
            otherFields = {
                    // Text字段默认不能排序，同时创建keyword子字段用于排序
                    @InnerField(suffix = "keyword", type = FieldType.Keyword),
                    @InnerField(suffix = "ngram", type = FieldType.Text, analyzer = "ngram_analyzer", searchAnalyzer = "ngram_search_analyzer"),
                    @InnerField(suffix = "edge", type = FieldType.Text, analyzer = "edge_ngram_analyzer", searchAnalyzer = "standard")
    })
    private String note;

    @Field(type = FieldType.Keyword)
    private String email;

    @Field(type = FieldType.Integer)
    private Integer age;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS", timezone = "GMT+8")
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second_millis)
//    @ValueConverter(LocaDateTimeEsConverter.class)
    private LocalDateTime createdAt;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS", timezone = "GMT+8")
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second_millis)
//    @ValueConverter(LocaDateTimeEsConverter.class)
    private LocalDateTime updatedAt;

    @Field(type = FieldType.Object, enabled = false)
    private Map<String, Object> metadata;

    // ES 9.x 支持新的字段类型
    @Field(type = FieldType.Search_As_You_Type)
    private String searchField;

    @Field(type = FieldType.Wildcard)
    private String wildcardField;
}